package com.ding.commander;

public class Commander {

    //整合所有的配置项
    private String password="mynat";

    private int minPoolSize = 2;

    private int maxPoolSize = 5;

    private int getWaiteTimeout = 3000;

    private int communicationListenerPort = 8888;

    private int serverListenerPort = 80;

    private int heartbeatTimeout = 4000;

    private int heartbeatTime = 2000;

    public void setServerListenerPort(int serverListenerPort) {
        this.serverListenerPort = serverListenerPort;
    }

    private CPool pool;

    private CommanderChannelHolder commanderHolder;

    private CommunicationAccepter socketAccepter;

    public void setPassword(String password) {
        this.password = password;
    }

    public void setMinPoolSize(int minPoolSize) {
        this.minPoolSize = minPoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public void setGetWaiteTimeout(int getWaiteTimeout) {
        this.getWaiteTimeout = getWaiteTimeout;
    }

    public void setCommunicationListenerPort(int communicationListenerPort) {
        this.communicationListenerPort = communicationListenerPort;
    }

    public void setHeartbeatTimeout(int heartbeatTimeout) {
        this.heartbeatTimeout = heartbeatTimeout;
    }

    public void setHeartbeatTime(int heartbeatTime) {
        this.heartbeatTime = heartbeatTime;
    }

    //开始工作
    public void start() throws Exception {


        commanderHolder = new CommanderChannelHolder();

        commanderHolder.setHeartbeatTime(this.heartbeatTime);

        commanderHolder.setHeartbeatTimeout(this.heartbeatTimeout);

        pool = new CPool();

        if (maxPoolSize <= 1) {
            throw new RuntimeException("通信池的数量上限必须大于1");
        }
        pool.setMaxSize(maxPoolSize);

        if (minPoolSize <= 0) {
            throw new RuntimeException("通信池的数量下限必须大于0");
        }
        pool.setMinSize(minPoolSize);

        if (getWaiteTimeout <= 0) {
            throw new RuntimeException("通信获取等待时长必须大于0");
        }
        pool.setGetWaiteTimeout(this.getWaiteTimeout);

        if (pool.getMaxSize() <= pool.getMinSize()) {
            throw new RuntimeException("通信池的数量上限必须大于下限");
        }

        //互相绑定
        pool.setCommanderHolder(commanderHolder);
        commanderHolder.setPool(pool);

        socketAccepter = new CommunicationAccepter();
        socketAccepter.setCommunicationListenerPort(this.communicationListenerPort);

        socketAccepter.setPool(pool);
        socketAccepter.setPassword(this.password);
        socketAccepter.setCommanderHolder(commanderHolder);

        socketAccepter.start();

        ServerAccepter server = new ServerAccepter();

        server.setPool(pool);

        server.setServerListenerPort(serverListenerPort);

        server.start();
    }


    public void stop() {

        //关闭通信接收器
        socketAccepter.stop();

        //关闭心跳等
        commanderHolder.stop();

        //关闭并清空所有通信
        pool.closeAll();

    }


}
